package com.njym.java.security.app.rest;


import com.njym.java.security.app.biz.CompanyIdentificationBiz;
import com.njym.java.security.app.biz.PersonalIdentificationBiz;
import com.njym.java.security.app.entity.CompanyIdentification;
import com.njym.java.security.app.entity.PersonalIdentification;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.service.CaptchaCodeManager;
import com.njym.java.security.common.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Controller
@RequestMapping("companyIdentification")
public class CompanyIdentificationController extends BaseController<CompanyIdentificationBiz,CompanyIdentification> {

    @Autowired
    private PersonalIdentificationBiz personalIdentificationBiz;

    @RequestMapping(value = "/submit", method = RequestMethod.GET)
    @ResponseBody
    public Object submit(String enterpriseName, String enterpriseNum, String enterpriseMain, String enterpriseUrl, String userId) {

        CompanyIdentification companyIdentification = new CompanyIdentification();
        if (!userId.equals("undefined") && userId != null) {
            Integer id = Integer.parseInt(userId);
            companyIdentification.setUserId(id);
        }

        Example example = new Example(CompanyIdentification.class);
        example.createCriteria().andEqualTo("userId", userId).andEqualTo("status", 2).andEqualTo("deleted", 0);
        List<CompanyIdentification> list = baseBiz.selectByExample(example);
        if(list.size() > 0){
            return ResponseUtil.fail(-1, "您已申请，无需重复申请！");
        }

        companyIdentification.setName(enterpriseName);
        companyIdentification.setRegisterNo(enterpriseNum);
        companyIdentification.setCompanyImage(enterpriseUrl);
        companyIdentification.setLegalName(enterpriseMain);
        companyIdentification.setStatus(1);

        Example example2 = new Example(CompanyIdentification.class);
        example2.createCriteria().andEqualTo("userId", userId).andEqualTo("status", 1).andEqualTo("deleted", 0);
        List<CompanyIdentification> com = baseBiz.selectByExample(example2);
        if(com.size() > 0){
            companyIdentification.setId(com.get(0).getId());
            baseBiz.updateSelectiveById(companyIdentification);
        }else{
            baseBiz.insertSelective(companyIdentification);
        }


        return ResponseUtil.ok();
    }

    @RequestMapping(value = "/changeIdentification", method = RequestMethod.GET)
    @ResponseBody
    public Object changeIdentification(String enterpriseName, String enterpriseNum, String enterpriseMain, String enterpriseUrl, String userId) {

        CompanyIdentification companyIdentification = new CompanyIdentification();
        if (!userId.equals("undefined") && userId != null) {
            Integer id = Integer.parseInt(userId);
            companyIdentification.setUserId(id);
        }
        companyIdentification.setName(enterpriseName);
        companyIdentification.setRegisterNo(enterpriseNum);
        companyIdentification.setCompanyImage(enterpriseUrl);
        companyIdentification.setLegalName(enterpriseMain);
        companyIdentification.setStatus(1);
        Example example = new Example(PersonalIdentification.class);
        example.createCriteria().andEqualTo("userId", userId).andEqualTo("status", 2).andEqualTo("deleted", 0);
        List<PersonalIdentification> per = personalIdentificationBiz.selectByExample(example);

        Example example1 = new Example(CompanyIdentification.class);
        example1.createCriteria().andEqualTo("userId", userId).andEqualTo("status", 2).andEqualTo("deleted", 0);
        List<CompanyIdentification> com = baseBiz.selectByExample(example1);

        if(com.size() > 0){
            companyIdentification.setId(com.get(0).getId());
            baseBiz.updateSelectiveById(companyIdentification);
        }else{
            if(per.size() > 0){
                PersonalIdentification p = per.get(0);
                p.setDeleted(1);
                personalIdentificationBiz.updateSelectiveById(p);
                baseBiz.insertSelective(companyIdentification);
            }
        }

        return ResponseUtil.ok();
    }

    @RequestMapping(value = "/queryapply", method = RequestMethod.GET)
    @ResponseBody
    public Object queryapply(String userId) {

        if (!userId.equals("undefined") && userId != null) {
            Integer id = Integer.parseInt(userId);

            Example example = new Example(CompanyIdentification.class);
            example.createCriteria().andEqualTo("userId", id).andEqualTo("status", 2).andEqualTo("deleted", 0);
            List<CompanyIdentification> list = baseBiz.selectByExample(example);

            Example example2 = new Example(PersonalIdentification.class);
            example2.createCriteria().andEqualTo("userId", id).andEqualTo("status", 2).andEqualTo("deleted", 0);
            List<PersonalIdentification> lists = personalIdentificationBiz.selectByExample(example2);

            if(list.size() == 0 && lists.size() == 0){
                return ResponseUtil.fail(-1, "未进行个人认证或个人认证正在审核中，请耐心等待！");
            }
        }


        return ResponseUtil.ok();
    }
}